package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiDZColor;
import com.hilaver.dzmis.basicinfo.BiYarn;
import com.hilaver.dzmis.codetable.CtYarnMaterial;
import com.hilaver.dzmis.codetable.CtYarnMeasure;
import com.hilaver.dzmis.product.ProductDZColor;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class ProductDZColorServiceImpl extends AbstractBaseService {
	
	private CtServiceImpl ctService;
	private I18nServiceImpl i18n;
	private BiDZColorServiceImpl biDZColorService;
	
	public ProductDZColorServiceImpl(){
		this.ctService = new CtServiceImpl();
		this.i18n = new I18nServiceImpl();
		this.biDZColorService = new BiDZColorServiceImpl();
	}
	
	public String edit(ProductDZColor obj) throws Exception {
		String operation = null;
		if(obj.getId() != null){
			operation = "02";
		}else{
			operation = "01";
		}
		
		ProductIdentification pi = (ProductIdentification)this.dao.get(ProductIdentification.class.getName(), obj.getProductIdentification().getId());
		if(!obj.getWeight().equals(pi.getWeight())){
			pi.setWeight(obj.getWeight().doubleValue());
			this.dao.saveOrUpdate(pi);
		}
		this.dao.saveOrUpdate(obj);
		super.editSysMbox(obj, operation);
		return "{success: true}";
	}

	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(ProductDZColor.class.getName(), id);
		return super.delete(ProductDZColor.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		ProductDZColor oz = (ProductDZColor)this.dao.get(ProductDZColor.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(oz));
		
		xmlSB.append("<dzColorId>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(oz.getBiDZColor().getId());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</dzColorId>");
		
		xmlSB.append("<dzColorCode>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(oz.getBiDZColor().getDzColorCode());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</dzColorCode>");
		
		xmlSB.append("<customerColorCode>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(oz.getBiDZColor().getCustomerColorCode()==null?"":oz.getBiDZColor().getCustomerColorCode());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</customerColorCode>");
		
		xmlSB.append("<yarnReference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(oz.getBiDZColor().getBiYarn().getReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</yarnReference>");
		
		xmlSB.append("<ctYarnMeasure>");
		xmlSB.append(Constants.XML_CDATA[0]);
		String yarnMeasure = this.ctService.getDescription(CtYarnMeasure.class.getName(), oz.getBiDZColor().getBiYarn().getCtYarnMeasure(), locale);
		xmlSB.append(yarnMeasure);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMeasure>");
		
		xmlSB.append("<yarnAccount>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(oz.getBiDZColor().getBiYarn().getYarnAccount());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</yarnAccount>");
		
		BiDZColor biDZColor = oz.getBiDZColor();
		xmlSB.append("<ctYarnMaterial1>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial1(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial1()) && biDZColor.getBiYarn().getComposition1() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition1() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial1>");
		
		xmlSB.append("<ctYarnMaterial2>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial2(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial2()) && biDZColor.getBiYarn().getComposition2() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition2() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial2>");
		
		xmlSB.append("<ctYarnMaterial3>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial3(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial3()) && biDZColor.getBiYarn().getComposition3() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition3() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial3>");
		
		xmlSB.append("<ctYarnMaterial4>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial4(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial4()) && biDZColor.getBiYarn().getComposition4() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition4() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial4>");
		
		xmlSB.append("<ctYarnMaterial5>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial5(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial5()) && biDZColor.getBiYarn().getComposition5() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition5() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial5>");
		
		if(biDZColor.getBiYarn().getBiFactory() != null){
			xmlSB.append("<factoryNameCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameCN>");
			
			xmlSB.append("<factoryNameEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameEN>");
			
			xmlSB.append("<addressFactoryCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getAddressFactoryCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryCN>");
			
			xmlSB.append("<addressFactoryEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getAddressFactoryEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryEN>");
		}
		
		this.biDZColorService.setRealFileDir(realFileDir);
		xmlSB.append("<photo3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.biDZColorService.getIsColorApprovedPhoto(biDZColor));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo3Name>");
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getWithDescription(int id) throws Exception {
		ProductDZColor oz = (ProductDZColor)this.dao.get(ProductDZColor.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(this.toXMLFragment(oz));

		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAllPagination(int page, String sort,
			String order, String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(ProductDZColor.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLFragment((ProductDZColor)obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductDZColor.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	
	public String getAll(Integer piId) throws Exception{
		String [] filters = new String[1];
		filters[0] = "productIdentification.id = " + piId;
		List objList = this.dao.getAll(ProductDZColor.class.getName(), "sequence", "desc", filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			xmlSB.append(this.toXMLFragment((ProductDZColor)obj));
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append("1");
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductDZColor.class.getName(),filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String toXMLFragment(ProductDZColor ob) throws Exception {
		StringBuffer xmlSB = new StringBuffer();
		
		xmlSB.append("<dzColorCode>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(ob.getBiDZColor().getDzColorCode());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</dzColorCode>");
		
		xmlSB.append("<customerColorCode>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(ob.getBiDZColor().getCustomerColorCode()==null?"":ob.getBiDZColor().getCustomerColorCode());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</customerColorCode>");
		
		xmlSB.append("<yarnReference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(ob.getBiDZColor().getBiYarn().getReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</yarnReference>");
		
		xmlSB.append("<yarnAccount>");
		xmlSB.append(Constants.XML_CDATA[0]);
		String yarnMeasure = this.ctService.getDescription(CtYarnMeasure.class.getName(), ob.getBiDZColor().getBiYarn().getCtYarnMeasure(), locale);
		xmlSB.append(yarnMeasure + "&nbsp;" + ob.getBiDZColor().getBiYarn().getYarnAccount());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</yarnAccount>");
		
		BiYarn yarn = ob.getBiDZColor().getBiYarn();
		xmlSB.append("<ctYarnMaterial>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(!StringUtils.isEmpty(yarn.getCtYarnMaterial1()) && yarn.getComposition1() != null){
			xmlSB.append("&nbsp;&nbsp;&nbsp;" + yarn.getComposition1() + "%&nbsp;");
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), yarn.getCtYarnMaterial1(), locale));
			
		}
		
		if(!StringUtils.isEmpty(yarn.getCtYarnMaterial2()) && yarn.getComposition2() != null){
			xmlSB.append("&nbsp;&nbsp;&nbsp;" + yarn.getComposition2() + "%&nbsp;");
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), yarn.getCtYarnMaterial2(), locale));
		}
		
		if(!StringUtils.isEmpty(yarn.getCtYarnMaterial3()) && yarn.getComposition3() != null){
			xmlSB.append("&nbsp;&nbsp;&nbsp;" + yarn.getComposition3() + "%&nbsp;");
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), yarn.getCtYarnMaterial3(), locale));
		}
		
		if(!StringUtils.isEmpty(yarn.getCtYarnMaterial4()) && yarn.getComposition4() != null){
			xmlSB.append("&nbsp;&nbsp;&nbsp;" + yarn.getComposition4() + "%&nbsp;");
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), yarn.getCtYarnMaterial4(), locale));
		}
		
		if(!StringUtils.isEmpty(yarn.getCtYarnMaterial5()) && yarn.getComposition5() != null){
			xmlSB.append("&nbsp;&nbsp;&nbsp;" + yarn.getComposition5() + "%&nbsp;");
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), yarn.getCtYarnMaterial5(), locale));
		}
		
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial>");
		
		this.biDZColorService.setRealFileDir(realFileDir);
		xmlSB.append("<photo3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.biDZColorService.getIsColorApprovedPhoto(ob.getBiDZColor()));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo3Name>");
		
		return xmlSB.toString();
	}
	
	public static void main(String[] args) throws Exception{
		ProductDZColorServiceImpl os = new ProductDZColorServiceImpl();
		System.out.println(os.getAll(2));
		//char index = 'C';
		//System.out.println(++index);
	}

}
